package com.caishi.lkx.exam.es.model;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.TypeReference;
import cn.hutool.json.JSONUtil;
import com.caishi.lkx.exam.deserializer.GenericEnumDeserializer;
import com.caishi.lkx.exam.deserializer.GenericEnumSerializer;
import com.caishi.lkx.exam.ienum.type.QuestionSourceType;
import com.caishi.lkx.exam.ienum.type.QuestionType;
import com.caishi.lkx.exam.model.QuestionModel;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import lombok.Getter;
import lombok.Setter;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;
import org.springframework.data.elasticsearch.annotations.Setting;

import java.util.List;
import java.util.Map;


@Getter
@Setter
@Document(indexName = "#{@esIndexName}_question")
@Setting(replicas = 0)
public class QuestionEs {
    /**
     * 必须指定ID
     */
    @Id
    private String id;

    /**
     * 科目id
     */
    @Field(type = FieldType.Integer)
    private Long categoryId;

    @Field(type = FieldType.Keyword)
    private String typeId;

    @Field(type = FieldType.Keyword)
    private String sourceId;

    @JsonSerialize(using = GenericEnumSerializer.class)  // 序列化为字符串
    @JsonDeserialize(using = GenericEnumDeserializer.class)  // 自定义反序列化
    private QuestionType firstType;  // 使用枚举字段

    @JsonSerialize(using = GenericEnumSerializer.class)  // 序列化为字符串
    @JsonDeserialize(using = GenericEnumDeserializer.class)  // 自定义反序列化
    private QuestionSourceType sourceType;

    @Field(type = FieldType.Text, analyzer = "ik_max_word", searchAnalyzer = "ik_smart")
    private String stem;

    @Field(type = FieldType.Text, analyzer = "ik_max_word", searchAnalyzer = "ik_smart")
    private String quOption;

    @Field(type = FieldType.Text, analyzer = "ik_max_word", searchAnalyzer = "ik_smart")
    private String answerDesc;

    @Field(type = FieldType.Text, analyzer = "ik_max_word", searchAnalyzer = "ik_smart")
    private String answer;

    public void setStem(List<Map<String, Object>> stem) {
        this.stem = JSONUtil.toJsonStr(stem);
    }

    public void setQuOption(List<Map<String, Object>> quOption) {
        this.quOption = JSONUtil.toJsonStr(quOption);
    }

    public void setAnswerDesc(List<Map<String, Object>> answerDesc) {
        this.answerDesc = JSONUtil.toJsonStr(answerDesc);
    }

    public void setAnswer(Object answer) {
        this.answer = JSONUtil.toJsonStr(answer);
    }

    public List<Map<String, Object>> getStem() {
        if(this.stem == null){
            return null;
        }
        return JSONUtil.toBean(this.stem, new TypeReference<>() {
        }, false);
    }

    public List<Map<String, Object>> getQuOption() {
        if(this.quOption == null){
            return null;
        }
        return JSONUtil.toBean(this.quOption, new TypeReference<>() {
        }, false);
    }

    public List<Map<String, Object>> getAnswerDesc() {
        if(this.answerDesc == null){
            return null;
        }
        return JSONUtil.toBean(this.answerDesc, new TypeReference<>() {
        }, false);
    }

    public Object getAnswer() {
        if (!JSONUtil.isJson(this.answer)) {
            return this.answer;
        }
        return JSONUtil.parse(this.answer);
    }

    public static QuestionEs modelTrans(QuestionModel model) {
        QuestionEs vo = new QuestionEs();
        BeanUtil.copyProperties(model, vo);
        vo.setAnswer(model.getAnswer());
        vo.setStem(model.getStem());
        vo.setQuOption(model.getQuOption());
        vo.setAnswerDesc(model.getAnswerDesc());
        return vo;
    }
    public static QuestionModel modelTransToModel(QuestionEs e) {
        var vo = QuestionModel.builder().categoryId(e.getCategoryId()).sourceId(e.getSourceId()).answer(e.getAnswer()).answerDesc(e.getAnswerDesc())
                .firstType(e.getFirstType()).quOption(e.getQuOption()).sourceType(e.getSourceType()).answerDesc(e.getAnswerDesc()).stem(e.getStem()).build();
        vo.setId(e.getId());
        return vo;
    }
}
